package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.OaNote;
import com.ruoyi.system.service.IOaNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 便签管理控制器
 *
 * @author 自定义
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/system/note")
public class OaNoteController extends BaseController {

    @Autowired
    private IOaNoteService oaNoteService;

    /**
     * 查询便签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OaNote oaNote) {
        startPage();
        List<OaNote> list = oaNoteService.selectOaNoteList(oaNote);
        return getDataTable(list);
    }

    /**
     * 根据ID获取便签详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(oaNoteService.selectOaNoteById(id));
    }

    /**
     * 新增便签
     */
    @PostMapping
    public AjaxResult add(@RequestBody OaNote oaNote) {
        // 设置创建时间为当前时间
        oaNote.setCreateTime(new Date());

        // 获取当前登录用户的 ID，假设若依框架提供了获取用户 ID 的方法
        Long userId = this.getUserId();
        oaNote.setUserId(userId);

        int rows = oaNoteService.insertOaNote(oaNote);
        if (rows > 0) {
            // 返回包含新便签 id 的数据
            System.out.println("便签添加成功，新便签 id: " + oaNote.getId());
            return AjaxResult.success("便签添加成功", oaNote.getId());
        } else {
            System.out.println("便签添加失败");
            return AjaxResult.error("便签添加失败");
        }
    }

    /**
     * 修改便签
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OaNote oaNote) {
        return toAjax(oaNoteService.updateOaNote(oaNote));
    }

    /**
     * 删除便签
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(oaNoteService.deleteOaNoteByIds(ids));
    }

    /**
     * 根据用户ID查询便签列表
     */
    @GetMapping("/user/{userId}")
    public TableDataInfo listByUserId(@PathVariable Long userId) {
        startPage();
        List<OaNote> list = oaNoteService.selectOaNoteListByUserId(userId);
        TableDataInfo dataTable = getDataTable(list);
        System.out.println("返回的便签列表数据: " + dataTable); // 添加日志输出
        return dataTable;
    }
}